{% extends 'fruitShop/base_user.html' %}
{% block body_content_inner %}
    {% load static %}

<div class="total_count">全部商品<em>2</em>件</div>
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
    {% for good in cartGoods %}
        <form action="{% url 'fruitShop:revieworder' %}" method="get">
	<ul class="cart_list_td clearfix" id="cart_id_{{ good.id }}">
		<li class="col01"><input value="{{ good.id }}" type="checkbox" name="selectme" id="check_cart_{{ good.id }}" onchange="tootal()" checked></li>
		<li class="col02"><img src="{% static 'fruitShop/upload_images/' %}{{ good.good.picture }}"></li>
		<li class="col03">{{ good.good.name }}<br><em>{{ good.good.price }}元/{{ good.good.unit }}</em></li>
		<li class="col04">{{ good.good.unit }}</li>
		<li class="col05"><span  id="good_price_{{ good.id }}">{{ good.good.price }}</span>元</li>
		<li class="col06">
			<div class="num_add">
				<a href="javascript:;" class="add fl" onclick="addNum_1(this,{{ good.id }})">+</a>
				<input type="text" class="num_show fl" value="{{ good.number }}" onchange="updateCartNum(this,{{ good.id }})">
				<a href="javascript:;" class="minus fl" onclick="reduceNum_1(this,{{ good.id }})">-</a>
			</div>
		</li>
		<li class="col07" id="all_cart_good_price_{{ good.id }}"></li>
		<li class="col08"><a onclick="deleteThisGood({{ good.id }})" href="javascript:;">删除</a></li>
	</ul>
{% endfor %}

	<ul class="settlements">
		<li class="col01"><input type="checkbox" onclick="selectMe(this)" checked="checked"></li>
		<li class="col02">全选</li>
		<li class="col03">合计(不含运费)：<span>¥</span><em id="total_price">42.60</em><br>共计<b id="total_cont">2</b>件商品</li>
		<li class="col04"><input type="submit" value="去结算" style="font-size:20px;color:beige;background: red;border: none;width: 100%;height: 78px;"></li>
	</ul>
</form>
{% endblock %}
{% block js %}
    <script>
    tootal()
    function tootal() {
        var total=0
        var total_count=0
        $('.col07').each(function () {
            checked=$(this).prev().prev().prev().prev().prev().prev().find('input')[0].checked
            if(checked){
                count=$(this).prev().find('input').val()
                price=$(this).prev().prev().text()
                total0=parseFloat(count)*parseFloat(price)
                $(this).text(total0.toFixed(2)+' 元')
                total+=total0
                total_count++
            }
        })
        $("#total_price").text(total)
        $("#total_cont").text(total_count)
    }
        function addNum_1(obj,id) {
            $(obj).next().val(parseInt($(obj).next().val())+1)
            updateCartNum($(obj).next()[0],id)
            tootal()
        }
        function reduceNum_1(obj,id) {
            if(parseInt($(obj).prev().val())>1){
                $(obj).prev().val(parseInt($(obj).prev().val())-1)
                updateCartNum($(obj).prev()[0],id)
                tootal()
            }
        }
        
        function updateCartNum(obj,id) {
             var number=parseInt($(obj).val());
                $.post('{% url 'fruitShop:editcart' %}',{
                    'id':id,
                    'number':number,
                    'csrfmiddlewaretoken':'{{ csrf_token }}'
                },function (res) {
                    if(res.status){
                        $(obj).val(number);
                        tootal()
                    }
                })
        }
        function selectMe(obj) {
            if($(obj)[0].checked==true){
                select()
            }else{
                noselect()
            }
            tootal()
        }

          /*全选，取消全选*/
        var checkall=document.getElementsByName("selectme");
        //全选
        function select(){
          for(var $i=0;$i<checkall.length;$i++){
            checkall[$i].checked=true;
          }
        };
        //全不选
        function noselect(){
          for(var $i=0;$i<checkall.length;$i++){
            checkall[$i].checked=false;
          }
        }
        //删除购物车中的商品
        function deleteThisGood(id) {
            if(confirm('确定要删除吗?')){
                $.post('{% url 'fruitShop:editcart' %}',{
                    'id':id,
                    'delete':1,
                    'csrfmiddlewaretoken':'{{ csrf_token }}'
                },function (res) {
                    if(res.status){
                        $("#cart_id_"+id).remove()
                        tootal()
                    }
                })
            }
        }
    </script>
{% endblock %}